package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JingfeiPici;
import com.ruoyi.system.service.IDeptService;
import com.ruoyi.system.service.IJingfeiDeptService;
import com.ruoyi.system.service.IJingfeiDetailService;
import com.ruoyi.system.service.IJingfeiPiciService;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 经费批次Controller
 *
 * @author ruoyi
 * @date 2023-02-16
 */
@Controller
@RequestMapping("/system/jingfeipici")
public class JingfeiPiciController extends BaseController
{
    private String prefix = "system/jingfeipici";

    @Autowired
    private IJingfeiPiciService jingfeiPiciService;

    @Autowired
    private IJingfeiDeptService jingfeiDeptService;

    @Autowired
    private IJingfeiDetailService jingfeiDetailService;

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("system:jingfeipici:view")
    @GetMapping()
    public String jingfeipici()
    {
        return prefix + "/jingfeipici";
    }

    //@RequiresPermissions("system:jingfeipici:manage")
    @GetMapping("/jingfeipicimanage")
    public String jingfeipicimanage()
    {
        return prefix + "/jingfeipici_manage";
    }

    @GetMapping("/chaxuntongji")
    public String chaxuntongji()
    {
        return prefix + "/chaxuntongji";
    }

    @GetMapping("/dangpicibofu")
    public String dangpicibofu()
    {
        return prefix + "/dangpicibofu";
    }

    @GetMapping("/jingfei_danweiqueren")
    public String jingfei_danweiqueren(String jingfeipiciid,String deptid,ModelMap map)
    {
        map.put("jingfeipiciid",jingfeipiciid);
        map.put("deptid",deptid);
        return prefix + "/jingfei_danweiqueren";
    }

    //@RequiresPermissions("system:jingfeibokuanjihua:view")
    @GetMapping("/jingfeibokuanjihua")
    public String jingfeibokuanjihua()
    {
        return  "/system/xitongguanli/jingfeibokuanjihua/index";
    }
    /**
     * 查询经费批次列表
     */
    @RequiresPermissions("system:jingfeipici:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiPici jingfeiPici)
    {
        startPage();
        List<JingfeiPici> list = jingfeiPiciService.selectJingfeiPiciList(jingfeiPici);
        return getDataTable(list);
    }

    /**
     * 导出经费批次列表
     */
    @RequiresPermissions("system:jingfeipici:export")
    @Log(title = "经费批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiPici jingfeiPici)
    {
        List<JingfeiPici> list = jingfeiPiciService.selectJingfeiPiciList(jingfeiPici);
        ExcelUtil<JingfeiPici> util = new ExcelUtil<JingfeiPici>(JingfeiPici.class);
        return util.exportExcel(list, "经费批次数据");
    }

    /**
     * 新增经费批次
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存经费批次
     */
//    @RequiresPermissions("system:jingfeipici:add")
    @Log(title = "经费批次", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiPici jingfeiPici)
    {
        jingfeiPici.setId(UUID.randomUUID().toString());
        jingfeiPici.setIsfafang(0);
        jingfeiPici.setIsjisuan("0");
        jingfeiPici.setIsvalid(1);
        return toAjax(jingfeiPiciService.insertJingfeiPici(jingfeiPici));
    }

    /**
     * 新增保存经费批次
     */
//    @RequiresPermissions("system:jingfeipici:add")
    @Log(title = "经费批次", businessType = BusinessType.INSERT)
    @PostMapping("/addcustom")
    @ResponseBody
    public AjaxResult addCustom(JingfeiPici jingfeiPici)
    {
        jingfeiPici.setId(UUID.randomUUID().toString());
        jingfeiPici.setIsfafang(0);
        jingfeiPici.setIsjisuan("0");
        jingfeiPici.setIsvalid(1);
        jingfeiPici.setIsdangqianpici(1);
        LocalDateTime ldt = jingfeiPici.getJieshunianyue().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMonths(1).minusSeconds(1);
        Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
        jingfeiPici.setJieshunianyue(date);
        LocalDateTime ldt2 = jingfeiPici.getJisuanshijian().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMonths(1).minusSeconds(1);
        Date date2 = Date.from(ldt2.atZone(ZoneId.systemDefault()).toInstant());
        jingfeiPici.setJisuanshijian(date2);
        return toAjax(jingfeiPiciService.insertJingfeiPici(jingfeiPici));
    }


    /**
     * 修改经费批次
     */
    @RequiresPermissions("system:jingfeipici:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JingfeiPici jingfeiPici = jingfeiPiciService.selectJingfeiPiciById(id);
        mmap.put("jingfeiPici", jingfeiPici);
        return prefix + "/edit";
    }

    /**
     * 修改保存经费批次
     */
    @RequiresPermissions("system:jingfeipici:edit")
    @Log(title = "经费批次", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiPici jingfeiPici)
    {
        return toAjax(jingfeiPiciService.updateJingfeiPici(jingfeiPici));
    }

    @Log(title = "经费批次", businessType = BusinessType.UPDATE)
    @PostMapping("/editcustom")
    @ResponseBody
    public AjaxResult editcustom(JingfeiPici jingfeiPici)
    {
        LocalDateTime ldt = jingfeiPici.getJieshunianyue().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMonths(1).minusSeconds(1);
        Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
        jingfeiPici.setJieshunianyue(date);
        LocalDateTime ldt2 = jingfeiPici.getJisuanshijian().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMonths(1).minusSeconds(1);
        Date date2 = Date.from(ldt2.atZone(ZoneId.systemDefault()).toInstant());
        jingfeiPici.setJisuanshijian(date2);
        return toAjax(jingfeiPiciService.modifyCustom(jingfeiPici));
    }

    /**
     * 删除经费批次
     */
    //@RequiresPermissions("system:jingfeipici:remove")
    @Log(title = "经费批次", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiPiciService.deleteJingfeiPiciByIds(ids));
    }

    @PostMapping( "/find_custom_list")
    @ResponseBody
    public JsonResult<?> findCustomList(@RequestParam("deptid")String ppDeptId, @RequestParam("picimingcheng")String ppPicimingcheng){
        try{
            List<Map<String,Object>> mmList = jingfeiPiciService.findCustomList(ppDeptId,ppPicimingcheng);
            return JsonResult.getSuccessResult(mmList);
        } catch (Exception e) {
            logger.error("jingfeipici -> find_custom_list: "+e.getMessage());
            return JsonResult.getErrorResult("查询经费批次失败");
        }
    }

    @PostMapping( "/find_custom_list_history")
    @ResponseBody
    public JsonResult<?> findCustomListHistory(@RequestParam("deptid")String ppDeptId, @RequestParam("picimingcheng")String ppPicimingcheng){
        try{
            List<Map<String,Object>> mmList = jingfeiPiciService.findCustomListHistory(ppDeptId,ppPicimingcheng);
            return JsonResult.getSuccessResult(mmList);
        } catch (Exception e) {
            logger.error("jingfeipici -> find_custom_list_history: "+e.getMessage());
            return JsonResult.getErrorResult("查询经费批次历史失败");
        }
    }

    @PostMapping( "/find_custom_list_now")
    @ResponseBody
    public JsonResult<?> findCustomListNow(@RequestParam("deptid")String ppDeptId, @RequestParam("picimingcheng")String ppPicimingcheng){
        try{
            List<Map<String,Object>> mmList = jingfeiPiciService.findCustomListNow(ppDeptId,ppPicimingcheng);
            return JsonResult.getSuccessResult(mmList);
        } catch (Exception e) {
            logger.error("jingfeipici -> find_custom_list_now: "+e.getMessage());
            return JsonResult.getErrorResult("查询经费批次当批次失败");
        }
    }

    @PostMapping( "/findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("id")String ppId){
        try{
            JingfeiPici mmJingfeipici = jingfeiPiciService.selectJingfeiPiciById(ppId);
            return JsonResult.getSuccessResult(mmJingfeipici);
        } catch (Exception e) {
            logger.error("jingfeipici -> findone: "+e.getMessage());
            return JsonResult.getErrorResult("查询经费批次失败");
        }
    }

    /**
     * 查询单位经费批次
     * @param deptId
     * @param picimingcheng
     * @return
     */
    @PostMapping( "/findDeptPici")
    @ResponseBody
    public JsonResult<?> getDeptPici(String deptId,String picimingcheng) {
        try{
            return JsonResult.getSuccessResult(jingfeiDeptService.selectDeptPiciByDeptid(deptId,picimingcheng));
        } catch (Exception e) {
            logger.error("jingfeiDept -> selectDeptPiciByDeptid: "+e.getMessage());
            return JsonResult.getErrorResult("查询单位经费批次失败");
        }
    }

    @PostMapping("/jisuan")
    @ResponseBody
    public JsonResult<?> jisuan(JingfeiPici jingfeiPici) {
        int result = jingfeiPiciService.jisuanPiciJingfei(jingfeiPici);
        if (result == 1)
            return JsonResult.getSuccessResult("计算成功");
        else if (result == 2)
            return JsonResult.getErrorResult("请先确认收款！");
        else
            return JsonResult.getErrorResult("计算失败");
    }

    @GetMapping("/jingfei_dept")
    public String jingfei_dept(@RequestParam("jingfeipiciid") String jingfeipiciid,@RequestParam("deptid") String deptid, ModelMap ppMap)
    {
        ppMap.put("jingfeipiciid",jingfeipiciid);
        ppMap.put("deptid",deptid);
        return prefix + "/jingfei_dept";
    }

    /**
     * 单位拨款详情 单位list
     */
    @PostMapping("/deptlist")
    @ResponseBody
    public TableDataInfo deptlist(@RequestParam Map<String, Object> map)
    {
        startPage();
        List<Map> list = deptService.selectJingfeiDeptList(map);
        return getDataTable(list);

    }
    /**
     * 单位拨款详情 专家list
     */
    @PostMapping("/zhuanjialist")
    @ResponseBody
    public TableDataInfo zhuanjialist(@RequestParam Map<String, Object> map)
    {
        startPage();
        List<Map> list = jingfeiDetailService.selectZhuanjiaList(map);
        return getDataTable(list);

    }

    @GetMapping("/jingfeipiciqueren")
    public String jingfeipiciqueren()
    {
        return prefix + "/jingfeipici_queren";
    }

    @GetMapping("/jingfei_queren")
    public String jingfei_queren(@RequestParam("jingfeipiciid") String jingfeipiciid,@RequestParam("deptid") String deptid, ModelMap ppMap)
    {
        ppMap.put("jingfeipiciid",jingfeipiciid);
        ppMap.put("deptid",deptid);
        return prefix + "/jingfei_queren";
    }

    //检查是否可以添加变动
    @GetMapping("/checkAdd")
    @ResponseBody
    public JsonResult<?> checkAdd(String deptid) {
        JingfeiPici jingfeiPici = new JingfeiPici();
        jingfeiPici.setDeptid(deptid);
        jingfeiPici.setIsjisuan("1");
        jingfeiPici.setIsfafang(0);
        List<JingfeiPici> jingfeiPiciList = jingfeiPiciService.selectJingfeiPiciList(jingfeiPici);
        int size = jingfeiPiciList.size();
        if (size ==0)
            return JsonResult.getSuccessResult("");
        else
            return JsonResult.getErrorResult("有未发放的批次不可以添加专家变动");
    }
}
